package com.zhz.lib.project.ibatis.dao.news.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.zhz.lib.project.ibatis.dao.news.NewsDAO;
import com.zhz.lib.project.ibatis.domain.news.News;

/**
 * @author LiWei <a href="mailto:liwei2672@gmail.com">liwei2672@gmail.com</a>
 * @version 创建时间：2010-5-12 下午09:42:16
 * 
 */
public class NewsDAOImpl extends SqlMapClientDaoSupport implements NewsDAO {
	private static Logger logger = Logger.getLogger(NewsDAOImpl.class);

	public News getNewsById(String id) {
		return (News) getSqlMapClientTemplate().queryForObject(
				"selectNewsById", id);
	}

	@SuppressWarnings("unchecked")
	public List<News> getTopNews(int firstResult, int maxResults) {
		Map map = new HashMap<String, Integer>();
		map.put("firstResult", firstResult);
		map.put("maxResults", maxResults);
		List list = null;
		try {
			list = getSqlMapClientTemplate().queryForList("getTopNews", map);
		} catch (DataAccessException e) {
			logger.error("getTopNews发生异常", e);
		}
		return list;
	}

	@SuppressWarnings("unchecked")
	public void newsHitPlusOne(String id) {
		Map map = new HashMap<String, Integer>();
		map.put("newsID", id);
		getSqlMapClientTemplate().update("newsHitPlusOne", map);
	}

	@SuppressWarnings("unchecked")
	public List<News> getLatestNews(int firstResult, int maxResults) {
		Map map = new HashMap<String, Integer>();
		map.put("firstResult", firstResult);
		map.put("maxResults", maxResults);
		List list = null;
		try {
			list = getSqlMapClientTemplate().queryForList("getLatestNews", map);
		} catch (DataAccessException e) {
			logger.error("getLatestNews发生异常", e);
		}
		return list;
	}

}
